package com.myopen.directive;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.jfinal.plugin.activerecord.Record;
import com.jfinal.template.Directive;
import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;
import com.myopen.config.ConstConfig;
import com.myopen.kit.RequestKit;

public class AuthDirective extends Directive{

	@SuppressWarnings("unchecked")
	@Override
	public void exec(Env env, Scope scope, Writer writer) {
		
		HttpServletRequest request=RequestKit.getTl().get();
		HttpSession session=request.getSession();
		
		Object[] obj=this.exprList.evalExprList(scope);
		String authCode=(String)obj[0];
		Record currUser=(Record)session.getAttribute(ConstConfig.SESSION_ADMIN_USER);
		Map<String,Record> sessionResourceCodes=(Map<String,Record>)currUser.get(ConstConfig.SESSION_ADMIN_RESOURCE_CODES);
		if(sessionResourceCodes.get(authCode)!=null) {
			stat.exec(env, scope, writer);
		}
		
	}

	public boolean hasEnd() {
		return true;
	}
	
}
